<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Report extends CI_Controller{
    public function index(){
        $month = isset($_REQUEST['month']);
        if(!$month){
            $month = date("Y-m",  strtotime("now"));
            $lastmonth = date("Y-m",  strtotime("-1 month"));
        }else{
            $month = $_REQUEST['month'];
            //var_dump($month);
            $tmp_year = substr($month,0,4);  
            $tmp_mon =substr($month,5,2); 
            $lastmonth = date("Y-m", mktime(0,0,0,$tmp_mon-1,1,$tmp_year));
        }
        $sql = "SELECT t1.homeid homeid,t2.checkinData checkinData,t2.homename homename,t2.charge charge,t1.dian dian,t1.shui shui,t1.kongtiao kongtiao "
                . "FROM `shuidian` as t1 join home as t2 on t1.homeid = t2.id where t1.yuedu = '".$month."' order by t1.homeid";
        $this->Dbconnect = $this->load->database('default',TRUE);
        $query = $this->Dbconnect->query($sql);
        $resDate = $query->result_array();
        $lastmonth_sql = "SELECT t1.homeid lastmonth_homeid,t2.homename lastmonth_homename,t2.charge lastmonth_charge,t1.dian lastmonth_dian,t1.shui lastmonth_shui,t1.kongtiao lastmonth_kongtiao "
                . "FROM `shuidian` as t1 join home as t2 on t1.homeid = t2.id where t1.yuedu = '".$lastmonth."' order by t1.homeid";
        $lastmonth_query = $this->Dbconnect->query($lastmonth_sql);
        $lastmonth_resDate = $lastmonth_query->result_array();
        //var_dump($resDate);
        //var_dump($lastmonth_resDate);
        $newData = $this->ArrayHeBing($resDate,$lastmonth_resDate);
        //header("Content-type: text/html; charset=utf-8");
        //var_dump($newData);exit;
        $resArr = array("data" => $newData); 
        $this->load->view("report",$resArr);
    }
    
    function ArrayHeBing($a,$b){
        $arr = array();
        foreach($a as $k=>$r){
            $arr[] = array_merge($r,$b[$k]);
            
        }
        return $arr;
    }
}